package com.warren.blog.testkafaka;

import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Properties;

/**
 * @author warren
 * @data: 2022/3/29 2:25 PM
 * @decription:
 */
public class CustomProducerCallBack {
    public static void main(String[] args) {
        //0配置
        Properties properties = new Properties();
        //连接集群bootstrap.servers
        properties.put("bootstrap.servers", "192.168.1.9:9092");
//        ProducerConfig.BOOTSTRAP_SERVERS_CONFIG
        //指定key和value的序列化类型key.serializer
//        ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG
//        ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
//        properties.put();
//        properties.put()

        //1.创建kafka生产者对象
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);
        //2.发送数据
        for (int i = 0; i < 5; i++) {
            //表名放入一个key，会在一个partition中
            kafkaProducer.send(new ProducerRecord<>("test1", "b","warren" + i), new Callback() {
                @Override
                public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                    if (e == null) {
                        System.out.println("主题: " + recordMetadata.topic() + "分区：" + recordMetadata.partition());
                    }
                }
            });
        }
        kafkaProducer.close();
    }
}
